package com.hs.telepass.manager;

import android.content.Context;
import com.hs.telepass.Constants;
import com.hs.telepass.MainApp;
import com.hs.telepass.dao.SingouProfilesDatabase;
import com.hs.telepass.dao.TelepassProfilesDatabase;
import com.hs.telepass.model.Profile;
import com.hs.telepass.model.SingouProfiles;
import com.hs.telepass.model.TelepassProfiles;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mei on 2014/9/29.
 */
public class ProfilesManager {
    private static ProfilesManager mInstance;
    private Context mContext;

    private List<SingouProfiles> mSingouProfilesList;
    private List<TelepassProfiles> mTelepassProfilesList;

    public ProfilesManager(){
        mContext = MainApp.getInstance();
    }

    public static ProfilesManager getInstance(){
        if(null == mInstance)
            mInstance = new ProfilesManager();
        return mInstance;
    }

    public List<Profile> getNewProfiles(){
        List<Profile> profileList = new ArrayList<Profile>();
        refreshSingouProfiles();
        refreshTelepassProfiles();
        for(TelepassProfiles telepassProfile : mTelepassProfilesList){
            Profile profile = new Profile();
            profile.setId(telepassProfile.getProfileID());
            profile.setUsername(telepassProfile.getUsername());
            profile.setTitle(telepassProfile.getHost());
            profile.setPassword(telepassProfile.getEncryptedPassword());
            profile.setSource(Constants.SOURCE_TELEFORM);
            profileList.add(profile);
        }

        for(SingouProfiles singouProfile : mSingouProfilesList){
            Profile profile = new Profile();
            profile.setId(singouProfile.getProfileID());
            profile.setTitle(singouProfile.getTile());
            profile.setImgPath(singouProfile.getLogoURL());
            profile.setUsername(singouProfile.getUsername());
            profile.setPassword(singouProfile.getEncrytedPassword());
            profile.setSource(Constants.SOURCE_SINGOU);
            profileList.add(profile);
        }

        return profileList;
    }

    /**
     * 根据条件获取登录历史
     * @param source
     *      用于该登陆用户属于singou还是teleform
     * @param hostname
     *      网站的域名信息
     *  @return 返回profle的List
     * */
    public List<Profile> getProfilesByTarget(int source, String hostname){
        List<Profile> profileList = getNewProfiles();
        List<Profile> resultsList = new ArrayList<Profile>();
        for(Profile profile : profileList){
            if(source == profile.getSource() && hostname.equals(profile.getTitle()))
                resultsList.add(profile);
        }

        return resultsList;
    }

    public List<SingouProfiles> refreshSingouProfiles(){
        mSingouProfilesList = new SingouProfilesDatabase(mContext).getProfiles();
        return mSingouProfilesList;
    }

    public List<TelepassProfiles> refreshTelepassProfiles(){
        mTelepassProfilesList = new TelepassProfilesDatabase(mContext).getProfiles();
        return mTelepassProfilesList;
    }

    public List<SingouProfiles> getSingouProfilesList(){
        return mSingouProfilesList;
    }

    public List<TelepassProfiles> getTelepassProfilesList(){
        return mTelepassProfilesList;
    }

    /**
     * 判断是否已经有账号密码完全相同的singou profile
     * */
    public boolean hasTotallySameSingouProfile(SingouProfiles profile){
        for(SingouProfiles loop_profile : getSingouProfilesList()){
            String loop_username = loop_profile.getUsername();
            String loop_passwrod = loop_profile.getEncrytedPassword();
            String loop_title = loop_profile.getTile();

            if(loop_username.equals(profile.getUsername()) && loop_passwrod.equals(profile.getEncrytedPassword())
                    && loop_title.equals(profile.getTile()))
                return true;

        }

        return false;
    }

    /**
     * 判断是否已经有账号密码完全相同的teleform profile
     * */
    public boolean hasTotallySameTeleProfile(TelepassProfiles profile){
        for(TelepassProfiles loop_profile : getTelepassProfilesList()){
            String loop_username = loop_profile.getUsername();
            String loop_passwrod = loop_profile.getEncryptedPassword();
            String loop_host = loop_profile.getHost();

            if(loop_username.equals(profile.getUsername()) && loop_passwrod.equals(profile.getEncryptedPassword())
                    && loop_host.equals(profile.getHost()))
                return true;

        }

        return false;
    }
}
